package io.zhifei.modules.sys.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.ToolUtils;
import io.zhifei.modules.sys.system.dao.MemberClassDao;
import io.zhifei.modules.sys.system.entity.MemberClassEntity;
import io.zhifei.modules.sys.system.form.*;
import io.zhifei.modules.sys.system.service.MemberClassService;
import io.zhifei.modules.sys.system.service.SysDictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 会员等级列表 服务实现类
 * </p>
 *
 * @author duke
 * @since 2020-10-15
 */
@Service
public class MemberClassServiceImpl extends ServiceImpl<MemberClassDao, MemberClassEntity> implements MemberClassService {

    @Resource
    private SysDictService dictService;
    @Override
    public void addMemberClass(MemberClassParam param) {
        MemberClassEntity memberClass = getEntity(param);
        this.baseMapper.insert(memberClass);
    }

    @Override
    public void updateMemberClass(MemberClassParam param) {
        MemberClassEntity entity = getEntity(param);
        MemberClassEntity oldEntity = getOldEntity(param);
        ToolUtils.copyProperties(entity,oldEntity);
        this.baseMapper.updateById(entity);
    }


    @Override
    public PageUtils getMemberClassPageList(Page page, MemberClassParam param) {
        IPage<MemberClassResult> memberClassPageList = this.baseMapper.getMemberClassPageList(page,param);
        List<MemberClassResult> memberClassResults = memberClassPageList.getRecords();
        for (MemberClassResult memberClassResult : memberClassResults) {
            String subjects = memberClassResult.getSubjects();
            if (StrUtil.isNotEmpty(subjects)){
                memberClassResult.setSubjectsArray(subjects.split(","));
            }
        }
        memberClassPageList.setRecords(memberClassResults);
        return new PageUtils(memberClassPageList);
    }

    @Override
    public List<MemberClassResult> getMemberClassList(MemberClassParam param) {
        return this.baseMapper.getMemberClassList(param);
    }

    /**
     * 获取班级名称列表，树形输出
     * @return
     */
    @Override
    public List<MemberClassTreeVo> getMemberClass2List() {
        //首先获得所有数据
        List<MemberClassTree> memberClassTrees = this.baseMapper.getMemberClass2List();
        //根据具体备考年份获得树形的根节点
        Set<String> classTreeRootSet = new LinkedHashSet<>();
        for(int i = 0 ; i < memberClassTrees.size() ; i++){
            classTreeRootSet.add(memberClassTrees.get(i).getBknf());
        }
        //制作树
        List<MemberClassTreeVo> memberClassTreeVos = new ArrayList<>();
        for (String key: classTreeRootSet) {
            MemberClassTreeVo memberClassTreeVo = new MemberClassTreeVo();
            memberClassTreeVo.setLabel(key);
            memberClassTreeVo.setChildren(getMemberClassChild2List(key,memberClassTrees));

            memberClassTreeVos.add(memberClassTreeVo);
        }
        return memberClassTreeVos;
    }

    private List<MemberClassTreeVo> getMemberClassChild2List(String key, List<MemberClassTree> memberClassTrees) {
        //制作子数据
        List<MemberClassTreeVo> memberClassTreeVos = new ArrayList<>();
        for(int j = 0 ; j < memberClassTrees.size() ; j++){
            if(key.equals(memberClassTrees.get(j).getBknf())){
                MemberClassTreeVo memberClassTreeVo = new MemberClassTreeVo();
                memberClassTreeVo.setLabel(memberClassTrees.get(j).getLabel());
                memberClassTreeVo.setValue(memberClassTrees.get(j).getValue());
                memberClassTreeVos.add(memberClassTreeVo);
            }

        }
        return memberClassTreeVos;
    }


    @Override
    public MemberClassResult getMemberClassDetail(MemberClassParam param) {
        return this.baseMapper.getMemberClassDetail(param);
    }

    @Override
    public List<UserCourseParam> getMemberClassCourse(Long classId) {
        return this.baseMapper.getMemberClassCourse(classId);
    }

    @Override
    public List<UserCourseParam> getSingleCourseByCourseId(Long packageCourseId) {
        return this.baseMapper.getSingleCourseByCourseId(packageCourseId);
    }

    @Override
    public PageUtils getCourseProgressStatistic(Page page, CourseProgressResult param) {
        IPage<CourseProgressResult> pageList = this.baseMapper.getCourseProgressStatistic(page,param);
        return new PageUtils(pageList);
    }

    @Override
    public List<CourseProgressResult> getCourseProgressStatisticList(CourseProgressResult param) {
        return this.baseMapper.getCourseProgressStatisticList(param);
    }


    /**
     * 转化对象
     * @param param 参数
     * @return
     */
    private MemberClassEntity getEntity(MemberClassParam param){
        MemberClassEntity entity = new MemberClassEntity();
        ToolUtils.copyProperties(param,entity);
        return entity;
    }

    /**
     * 获取旧的数据
     * @param param
     * @return
     */
    private MemberClassEntity getOldEntity(MemberClassParam param) {
        return this.getById(param.getClassId());
    }
}
